package com.itheima.day10.ZuoYe;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ZuoYe5 {
    private static final int count = 5;
    public static void encrypt(String file,String target){
        try (FileInputStream fis = new FileInputStream(file);
             FileOutputStream fos = new FileOutputStream(target)) {

            int data;
            while ((data = fis.read()) != -1) {
                fos.write((data + count) % 256); // 加密：偏移
            }
            System.out.println("加密完成！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void decrypt(String sourse, String target) {
        try (FileInputStream fis = new FileInputStream(sourse);
             FileOutputStream fos = new FileOutputStream(target)) {

            int data;
            while ((data = fis.read()) != -1) {
                fos.write((data - count + 256) % 256); // 解密：反向偏移
            }
            System.out.println("解密完成！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        File dir1 = new File("D:\\a");
    }
}
//public class SimpleByteShiftEncryption {
//    private static final int SHIFT = 5; // 偏移量
//
//    public static void encrypt(String source, String target) {
//        try (FileInputStream fis = new FileInputStream(source);
//             FileOutputStream fos = new FileOutputStream(target)) {
//
//            int data;
//            while ((data = fis.read()) != -1) {
//                fos.write((data + SHIFT) % 256); // 加密：偏移
//            }
//            System.out.println("加密完成！");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static void decrypt(String source, String target) {
//        try (FileInputStream fis = new FileInputStream(source);
//             FileOutputStream fos = new FileOutputStream(target)) {
//
//            int data;
//            while ((data = fis.read()) != -1) {
//                fos.write((data - SHIFT + 256) % 256); // 解密：反向偏移
//            }
//            System.out.println("解密完成！");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static void main(String[] args) {
//        encrypt("source.txt", "encrypted.txt");
//        decrypt("encrypted.txt", "decrypted.txt");
//    }
//}